package com.guge.sports.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guge.sports.entity.Dto.Option;
import com.guge.sports.entity.Dto.SelectionCourseHappeningDto;
import com.guge.sports.entity.Dto.StCourseDto;
import com.guge.sports.entity.StCourse;
import com.guge.sports.entity.StTeacher;
import com.guge.sports.mapper.StCourseMapper;
import com.guge.sports.mapper.StTeacherMapper;
import com.guge.sports.service.StCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guge.sports.toolPackage.base.BaseService;
import com.guge.sports.toolPackage.utils.MyPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-09-14
 */
@Service
public class StCourseServiceImpl extends BaseService<StCourse> implements StCourseService {

    @Autowired
    private StCourseMapper courseMapper;

    @Autowired
    private StTeacherMapper teacherMapper;




    /**
     * 分页查询课程
     * @param myPager
     * @param courseDto
     * @return
     */
    @Override
    public MyPager selectCoursePage(MyPager<StCourseDto> myPager, StCourseDto courseDto) {

        List<StCourseDto> stCourseDtos = courseMapper.selectStCoursePage(myPager.calcuStartIndex(),courseDto);
        int count = courseMapper.selectCountStTeacherPage(courseDto);

        return myPager.setTotalCount(count).calcuTotalPage().setList(stCourseDtos);

    }

    /**
     * 新增课程
     * @param course
     */
    @Override
    public void addCourse(StCourse course) {
        this.insert(course);
    }

    @Override
    public MyPager selectionCourse(MyPager<StCourseDto> myPager, StCourseDto stCourseDto) {
        List<StCourseDto>  stCourseDtos= courseMapper.selectionCourse(myPager.calcuStartIndex(),stCourseDto);
        int count = courseMapper.selectCountStTeacherPage(stCourseDto);
        return myPager.setTotalCount(count).calcuTotalPage().setList(stCourseDtos);
    }

    @Override
    public List<SelectionCourseHappeningDto> selectionCourseHappening() {
        return null;
    }

    /**
     * 查询下拉框的属性
     * @return
     */
    @Override
    public List<Option> getTeacherName() {
        QueryWrapper<StTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("tid","tname").eq("state",1);
        List<StTeacher> teachers = teacherMapper.selectList(queryWrapper);
        List<Option> list = new ArrayList<>();
        teachers.forEach((teacher)->{
            Option option = new Option();
            option.setValue(teacher.getTid().toString());
            option.setLabel(teacher.getTname());
            list.add(option);
        });
        return list;
    }

    /**
     * 下架课程
     * @param id
     */
    @Override
    public void courseTakeDown(Long id) {
        courseMapper.courseTakeDown(id);
    }

    /**
     * 软删除
     * @param id
     */
    @Override
    public void deleteCourse(Long id) {
      courseMapper.deleteCourse(id);
    }

    /**
     * 根据id查询课程和老师姓名
     * @param id
     * @return
     */
    @Override
    public StCourseDto getCourseById(Long id) {
        StCourseDto stCourseDto = courseMapper.getCourseById(id);
        return stCourseDto;
    }


}
